<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>鼓励金购买</title>
		<link rel="stylesheet" href="../../wx/css/iconfont.css" />
	    <link rel="stylesheet" href="../../wx/css/new.css" />
	    <link rel="stylesheet" href="../../wx/css/style.css" />
	    <link rel="stylesheet" href="../../wx/css/mall.css" />
	    <link rel="stylesheet" href="../../css/mui.min.css" rel="stylesheet" />
</head>
<body>
	<header class="mui-bar mui-bar-nav">
	    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
	    <h1 class="mui-title">电子优惠券</h1>
	</header>
	<div class="mui-content gulijin_content">
	    <div class="dzyhq_top">
	    	<div class="dzyhq_tbox">
	    		<p class="glj_name"><em>鼓励金余额</em></p>
		    	<p class="pri">¥<em id="score"></em></p>
		    	<p class="id">ID:<em id="id"></em></p>
	    	</div>
	    </div>
	    <div><img src="__INDEX__/images/bt_dzhyq.jpg"></div>
	    <ul class="dzyhq_list">
	    	<li data-val=10 class="active">
	    		<div class="left">¥<em>10</em></div>
	    		<div class="right">
    				<p class="num">40</p>
    				<p>鼓励金</p>
	    		</div>
	    	</li>
	    	<li data-val=50>
	    		<div class="left">¥<em>50</em></div>
	    		<div class="right">
    				<p class="num">200</p>
    				<p>鼓励金</p>
	    		</div>
	    	</li>
	    	<li data-val=100>
	    		<div class="left">¥<em>100</em></div>
	    		<div class="right">
    				<p class="num">400</p>
    				<p>鼓励金</p>
	    		</div>
	    	</li>
	    </ul>
	    <form class="mui-input-group">
			<div class="mui-input-row mui-radio mui-left">
				<label class="dzyhq_xiey"><a href="xieyi.html?type=cz">鼓励金服务协议</a></label>
				<input name="checkbox" id="checkbox" value="Item 1" type="radio" >
			</div>
		</form>
		
	    <button type="button" id="btn" class="dzhy_nr" onclick="topay()">确定购买</button>
	</div>
	 <div id="picture" class="mui-popover mui-popover-action mui-popover-bottom">
			<ul class="mui-table-view">
				<li class="mui-table-view-cell">
					<a href="#" onclick="pay('alipay')">支付宝支付</a>
				</li>
				<li class="mui-table-view-cell">
					<a href="#" onclick="pay('wxpay')">微信支付</a>
				</li>
			</ul>
			<ul class="mui-table-view">
				<li class="mui-table-view-cell">
					<a href="#picture"><b>取消</b></a>
				</li>
			</ul>
		</div>
</body>
<script src="../../js/mui.js"></script>
<script src="../../js/vue.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/app.js"></script>
<script src="../../js/m.js"></script>
<script src="../../js/common.js"></script>
<script>
	var cz=10
	mui('.dzyhq_list').on('tap', 'li', function() {
		document.getElementsByClassName("active")[0].classList.remove('active');  
		this.classList.add("active");
		cz=this.getAttribute("data-val");		
	});
	function topay(){
		mui('.mui-popover-action').popover('toggle');
		return;
		   var box = document.getElementById("checkbox");
			if(!box.checked) {
				   mui.alert('请阅读购买协议', '购买提示', function() {					
				  });
				    return;
			}
			var btnArray = ['不同意', '同意'];
				mui.confirm('鼓励金是一种电子优惠券,一经购买概不退回。您已知晓购买的鼓励金是用作消费时抵部分现金使用，并已确认了附近有合作实体店铺,如果附近没有实体店可供您消费,请勿购买', '鼓励金电子优惠券购买须知', btnArray, function(e) {
				 if (e.index == 1) {
				 	
				 	mui('.mui-popover-action').popover('toggle');
				 }
			})
	}
var pays={};
function plusReady(){
	// 获取支付通道
	plus.payment.getChannels(function(channels){
		var content=document.getElementById('dcontent');
		var info=document.getElementById('info');
		var txt='支付通道信息：';
		for(var i in channels){
			var channel=channels[i];
			if(channel.id=='qhpay'||channel.id=='qihoo'){	// 过滤掉不支持的支付通道：暂不支持360相关支付
				continue;
			}
			pays[channel.id]=channel;
			txt += 'id:'+channel.id+', ';
			txt += 'description:'+channel.description+', ';
			txt += 'serviceReady:'+channel.serviceReady+'； ';
			
		}
		info.innerText=txt;
	},function(e){
		outLine('获取支付通道失败：'+e.message);
	});
}
document.addEventListener('plusready', plusReady, false);
// 检测是否安装支付服务
function checkServices(pc){
	if(!pc.serviceReady){
		var txt=null;
		switch(pc.id){
			case 'alipay':
			txt='检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？';
			break;
			default:
			txt='系统未安装“'+pc.description+'”服务，无法完成支付，是否立即安装？';
			break;
		}
		plus.nativeUI.confirm(txt, function(e){
			if(e.index==0){
				pc.installService();
			}
		}, pc.description);
	}
}

var w=null;
var PAYSERVER='http://demo.dcloud.net.cn/payment/?payid=';
// 支付
function pay(id){
	if(w){return;}//检查是否请求订单中
	if(id==='appleiap'){
		//outSet('应用内支付');
		clicked('payment_iap.html');
		return;
	}
	console.log(4444444444444444444444444444444444444444);
	//outSet('----- 请求支付 -----');
	var url=PAYSERVER;
	if(id=='alipay'||id=='wxpay'){
		url+=id;
	}else{
		plus.nativeUI.alert('当前环境不支持此支付通道！', null, '捐赠');
		return;
	}
	var appid=plus.runtime.appid;
	if(navigator.userAgent.indexOf('StreamApp')>=0){
		appid='Stream';
	}
	url+='&appid='+appid+'&total=';
	console.log(3333+url);
	w=plus.nativeUI.showWaiting();
	// 请求支付订单
	var amount = 1;
	var xhr=new XMLHttpRequest();
	xhr.onreadystatechange=function(){
		switch(xhr.readyState){
			case 4:
			w.close();w=null;
			if(xhr.status==200){
				console.log('----- 请求订单成功 -----');
				console.log(xhr.responseText);
				var order=xhr.responseText;
				plus.payment.request(pays[id],order,function(result){
					console.log('----- 支付成功 -----');
					outLine(JSON.stringify(result));
					plus.nativeUI.alert('支付成功：感谢你的支持，我们会继续努力完善产品。',function(){
						back();
					},'捐赠');
				},function(e){
					console.log('----- 支付失败 -----');
					console.log('['+e.code+']：'+e.message);
					plus.nativeUI.alert('更多错误信息请参考支付(Payment)规范文档：http://www.html5plus.org/#specification#/specification/Payment.html', null, '支付失败：'+e.code);
				});
			}else{
				//outLine('----- 请求订单失败 -----');
				console.log( xhr.status );
				plus.nativeUI.alert('获取订单信息失败！', null, '捐赠');
			}
			break;
			default:
			break;
		}
	}
	xhr.open('GET',url+amount);
	console.log('请求支付订单：'+url+amount);
	xhr.send();
}
		</script>
	
</html>
